<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>
      <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
        <detail-swiper :topImages="topImages" ></detail-swiper>
        <detail-base-info :goods="goods"></detail-base-info>
        <detail-shop-info :shop="shop"></detail-shop-info>
        <detail-goods-info :detail-info="detailInfo"  @imageLoad="imageLoad"></detail-goods-info>
        <detail-param-info ref="params" :paramInfo="paramInfo"></detail-param-info>
        <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
        <goods-list ref="recommend" :goods="recommends"></goods-list>
       
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    
  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from './childComps/DetailBaseInfo';
  import DetailShopInfo from './childComps/DetailShopInfo';
  import DetailGoodsInfo from './childComps/DetailGoodsInfo'
  import DetailParamInfo from './childComps/DetailParamInfo'
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailBottomBar from './childComps/DetailBottomBar'

  import Scroll from '@/components/common/scroll/Scroll';
  import GoodsList from '@/components/content/goods/GoodsList'
  import BackTop from "@/components/content/backTop/BackTop"; 
  // import Toast from '@/components/common/toast/Toast'

  import {getDetail,Goods,Shop,GoodsParam,getRecommend} from '@/network/detail';

  import { mapActions } from 'vuex'
  // import { debounce } from '@/common/utils';
  // import {itemListenerMixin, backTopMixin} from "@/common/mixin";
export default {
  name:'Detail',
  components:{
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    Scroll,
    GoodsList,
    BackTop,
    // Toast
  },
  destoyed(){
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  },
  data(){
    return {
      iid:null,
      topImages:[],
      goods: {},
      shop: {},
      detailInfo:{},
      paramInfo: {},
      commentInfo:{},
      recommends:[],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex:0,
      isShowBackTop:false,
      /* message:'',
      show:false */
    }
  },
  // mixins: [itemListenerMixin, backTopMixin],
  created(){
    // 1.保存存入的ild
    this.iid = this.$route.params.iid;

    // 2.根据ild请求详情数据
   getDetail(this.iid).then(res =>    {
     console.log(res);
     // 1.获取顶部的图片轮播数据
     const data = res.result;
     this.topImages = res.result.itemInfo.topImages;

     // 2.获取商品信息
     this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);

     // 3.创建店铺信息的对象
     this.shop = new Shop(data.shopInfo)

     // 4.保存商品的详情数据
     this.detailInfo = data.detailInfo;

     // 5.获取参数的信息
     this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)

     // 6.取出评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }


      /* this.$nextTick(() => {
        // 根据最新的数据 对应的DOM是已经被渲染出来
        // 但是图片还是没加载完的
         this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        console.log(this.themeTopYs);
        
      }) */
    
   })

    // 3.请求推荐数据
    getRecommend().then(res => {
        //  console.log(res);
        this.recommends = res.data.list;
        
      })
     
      // 给getThemeTopY赋值(对给this.themeTopYs赋值的操作进行防抖)
      /* this.getThemeTopY = debounce(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        this.themeTopYs.push(Number.MAX_SAFE_INTEGER);
        console.log(this.themeTopYs);
        }, 100) */
        
      
  },

  
  methods: {
    ...mapActions(['addCart']),
    imageLoad(){
      this.$refs.scroll.refresh()
    },
    titleClick(index){
      console.log(index);
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
    },
    contentScroll(position) {
      // 1.获取Y值
      const positionY = -position.y

      // 2.positionY和主题中值进行对比
      let length = this.themeTopYs.length;
      for(let i = 0; i < length - 1; i++){
        if(this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])){
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      // 3.是否显示回到顶部
        this.isShowBackTop = (-position.y) > 1000;
      // this.listenShowBackTop(position);
      /* for(let i in this.themeTopYs){
        if (positionY > this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) {
          console.log(i);
          this.$refs.nav.currentIndex = this.currentIndex
        }
      } */
      // console.log(position);
    },
    //点击向上跳转按钮
      backClick(){
        this.$refs.scroll.scrollTo(0,0)
      },

    detailImageLoad() {
      // this.newRefresh();

      this.getThemeTopY();
    },

    addToCart(){
      // 1.创建对象
      const product = {};
      // 2.获取购物车需要展示的信息
      
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      // console.log('1111111');

      // 2.将商品添加到购物车里(1.promise 2.mapActions)
      // this.$store.cartList.push(product)
      // this.$store.commit('addCart',product)
      this.addCart(product).then(res =>{
        /* this.show = true;
        this.message = res;

        setTimeout(() => {
          this.show = false;
          this.message = '';
        }, 1500);
        console.log(res); */
        this.$toast.show(res,2000)
      })// 2.

      /* this.$store.dispatch('addCart',product).then(res => {
        console.log(res);
      })// 1. */
    }
  },

  mounted() {
    setTimeout(() => {
      this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44)
        this.themeTopYs.push(Number.MAX_SAFE_INTEGER);
        console.log(this.themeTopYs);
    }, 300);
    // console.log('mounted');
  },

  updated() {
   
  },
}

</script>

<style scoped>
  #detail {
    height: 100vh;
    position: relative;
    z-index: 99;
    background-color: #fff;
  }

  .detail-nav {
    position: relative;
    z-index: 99;
    background: #fff;
  }

  .content {
    /* position: absolute;
    top: 44px;
    bottom: 60px; */
    height: calc(100% - 44px); 
    overflow: hidden;
  }
</style>